package bid.web;

import bid.dao.DepartDao;
import bid.dao.UserDao;
import bid.dao.UserRoleDao;
import bid.dao.impl.DepartDaoImpl;
import bid.dao.impl.UserDaoImpl;
import bid.dao.impl.UserRoleDaoImpl;
import bid.entity.Department;
import bid.entity.Page;
import bid.entity.User;
import bid.entity.UserRole;
import bid.util.StringUtils;
import com.alibaba.fastjson2.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//完成用户功能的通用servlet
@WebServlet(value = "*.dept")
public class DepartServlet extends HttpServlet {
    DepartDao departDao = new DepartDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);//保持一致
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String path = req.getServletPath();//请求路径

        path = path.substring(1, path.length() - 5);

        if ("add".equals(path)) {
            add(req, resp);
        } else if ("update".equals(path)) {
            update(req, resp);
        } else if ("del".equals(path)) {
            del(req, resp);
        } else if ("search".equals(path)) {
            search(req, resp);
        }else if ("find".equals(path)) {
            find(req, resp);
        }  else if ("toAdd".equals(path)) {
            toAdd(req, resp);
        } else if ("toUpdate".equals(path)) {
            toUpdate(req, resp);
        } else {
            resp.getWriter().println("这是未知操作");
        }
    }


    public void search(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = StringUtils.toInt(request.getParameter("pageNo"), 1);
        int pageSize = Page.PAGESIZE;
        Page<Department> page = departDao.paging(pageNo, pageSize);
        request.setAttribute("p", page);
        request.getRequestDispatcher("/departments.jsp").forward(request, resp);

    }
    //返回ajax
    public void find(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        // [{"id":1,"name":"xxxx"},{}]
        List<Department> all = departDao.findAll();
        //all转换成json
        String s = JSON.toJSONString(all);
//        try {
//            Thread.sleep(8000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        resp.getWriter().print(s);//!!!!!!!!!!!!!!!!
    }
    public void add(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        Department user = new Department();
        user.setDepartmentname(request.getParameter("departmentname"));
        int p = departDao.add(user);
        if (p > 0) {
            resp.getWriter().print("<script>alert('新增成功');window.location='search.dept';</script>");
            return;
        }
        resp.getWriter().print("<script>alert('新增失败');window.history.back();</script>");

    }
    public void del(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        long id = Long.parseLong(request.getParameter("id"));

        int r = this.departDao.del(id);
        if (r > 0) {
            resp.sendRedirect("search.dept");///!!!!!!!!11
            return;
        }
        resp.getWriter().print("<script>alert('删除失败');window.history.back();</script>");
    }

    //跳转到新增的jsp
    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.getRequestDispatcher("/adddepartment.jsp").forward(req, resp);

    }

    //跳转到修改的jsp
    public void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long id = Long.parseLong(req.getParameter("id"));
        Department d = departDao.get(id);
        req.setAttribute("depart", d);


        req.getRequestDispatcher("/updatedept.jsp").forward(req, resp);

    }

    public void update(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        Department user = new Department();
        user.setId(Long.parseLong(request.getParameter("id")));
        user.setDepartmentname(request.getParameter("deptname"));

      int r = departDao.update(user);
      if (r > 0) {
           resp.getWriter().print("<script>alert('修改成功');window.location='search.dept';</script>");
           return;
       }
       resp.getWriter().print("<script>alert('修改失败');window.history.back();</script>");

    }


}
